﻿var submitFormHandler;

jQuery(document).ready(function(){
            
            // adds the style to grid     
            var theGrid = $('.grid');
            theGrid.find('tr th:first').addClass("topleft");
            theGrid.find('tr th:last').addClass("topright");
            
            theGrid.find('tr:last td:first').addClass("botleft");
            theGrid.find('tr:last td:last').addClass("botright");
            
            // delete confirmation box
            $('#deleteDialog').html('Are you sure you want to delete this item ?');
            $('#deleteDialog').dialog({
                autoOpen: false,
                modal: true,
                resizable: false,
                buttons: {
                    'Yes': function() { 
                        $(this).dialog('close'); 
                        $("form[id='"+ submitFormHandler +"']").submit(); },
                    'No': function() { $(this).dialog("close"); }
                }
            });
            
            // creates the flush message
            $("p[id='pageMessage']")
                .css("background-color","#F6A828")
                .css("color","white")
                .css("padding","6px")
                .center().addClass("portlet-header ui-corner-all")
                .fadeTo(1000, 1).fadeOut(2000);
            
            // search dialogbox
            $(".searchForm").dialog({
			    bgiframe: true,
			    autoOpen: false,
			    height: 400,
			    width:450,
			    modal: true,
			    buttons: {
				    'Search': function() {
						$(this).dialog('close');
						
						var hasFirstName = ! $('.firstName').is(':empty');
						var hasSurename = ! $('.surename').is(':empty');
						var hasEmail = ! $('.email').is(':empty');
						var hasPhone = ! $('.phone').is(':empty');
						
						if(hasFirstName || hasSurename || hasEmail || hasPhone)
						    $("#searchFormCriteria").submit();
				    },
				    Cancel: function() {
					    $(this).dialog('close');
				    }
			    },
			    close: function() {
				    //allFields.val('').removeClass('ui-state-error');
			    }
		    });
            
            $('.search').click(function() {
			    var searchBox = $('.searchForm');
			    searchBox.dialog('open');
		    });
                
            $(this).find("form").each(function(i) {
                var id = $(this).attr("id");
                
                if(id == "")    
                    {
                        var actionFieldSet = document.createElement("fieldset");
                        
                        $(this).wrapInner("<fieldset></fieldset>");
                        $(this).append(actionFieldSet);
                        
                        $("form button").appendTo(actionFieldSet);
                        $("input[type=submit]").appendTo(actionFieldSet);

                        $(this).addClass("niceform");
                        
                        $(this).find("label").each(function(i){
                            var dt = $(this);
                            var dd = $(this).next("input,select,textarea");
                            
                            dt.wrap("<dt></dt>");
                            dd.wrap("<dd></dd>");
                            
                            var dl = dt.parent();
                            dl.wrap("<dl></dl>");
                            dd.parent().appendTo(dl.parent());
                        });
                    }
            });
        });
        
        
function ConfirmeDialog(button) {
    submitFormHandler = button;
    $("#deleteDialog").dialog("open"); 
}

jQuery.fn.center = function (absolute) {
    return this.each(function () {
        var t = jQuery(this);

        t.css({
            position:    absolute ? 'absolute' : 'fixed', 
            left:        '50%', 
            top:        '50%', 
            zIndex:        '99'
        }).css({
            marginLeft:    '-' + (t.outerWidth() / 2) + 'px', 
            marginTop:    '-' + (t.outerHeight() / 2) + 'px'
        });

        if (absolute) {
            t.css({
                marginTop:    parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
                marginLeft:    parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
            });
        }
    });
};